﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tema2_Adabd.BusinessLayer;

namespace Tema2_Adabd
{
    public partial class MainForm : Form
    {
        private Utilizator utilizator;

        private void clkLogin(object sender, EventArgs e)
        {
            //txtNumeUtilizator.Text = "Radu";
            //txtParola.Text = "1000:SV7IZ1tFPMolGVeu9r2dB4YE4n0qurLb:zPVsKpG+MpnsDiu8S9bmW6NcjK74F//J";

            if (txtNumeUtilizator.Text == "" || txtParola.Text == "")
            {
                MessageBox.Show("Nu ati completat un camp.");
                return;
            }

            try
            {
                utilizator = Utilizator.autentificaUtilizator(txtNumeUtilizator.Text, txtParola.Text);
                if (utilizator == null)
                {
                    MessageBox.Show("Numele utilizatorul este incorect sau parola este gresita.");
                    return;
                }
                if (utilizator.isAdmin() == true)
                {
                    administrareConturiToolStripMenuItem.Visible = true;
                    administrareConturiToolStripMenuItem.Enabled = true;
                }
                curataFereastraDupaAutentificare();
                activeazaControale();
                MessageBox.Show("Autentificare cu succes.","",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch (KeyNotFoundException)
            {
                MessageBox.Show("Parola este gresita.");
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
            finally
            {
                txtParola.Text = "";
                txtNumeUtilizator.Text="";
            }
        }

        private void clkLogout(object sender, EventArgs e)
        {
            dezactiveazaControale();
            activeazaControaleAutenficare();
        }

        private void clkLogVizitator(object sender, EventArgs e)
        {
            utilizator = Utilizator.guest();

            curataFereastraDupaAutentificare();
            activeazaControaleVizitator();
        }

        private void clkCancel(object sender, EventArgs e)
        {
            DialogResult dr=MessageBox.Show("Renuntati la autentificare?","",MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void clkAdaugaCont(object sender, EventArgs e)
        {
            AdaugaUtilizator ad = new AdaugaUtilizator(utilizator, false);
            ad.Show();
        }

        private void clkStergeCont(object sender, EventArgs e)
        {
            AdaugaUtilizator ad = new AdaugaUtilizator(utilizator, true);
            ad.Show();
        }

        private void curataFereastraDupaAutentificare()
        {
            txtNumeUtilizator.Visible = false;
            txtNumeUtilizator.Enabled = false;
            txtParola.Visible = false;
            txtParola.Enabled = false;

            btnLogareCaVizitator.Enabled = false;
            btnLogareCaVizitator.Visible = false;
            btnLogin.Visible = false;
            btnLogin.Enabled = false;
            btnLogout.Visible = false;
            btnLogin.Enabled = false;

            lbAutentificare.Visible = false;
            lbAutentificare.Enabled = false;
            lbTextAutentificare.Visible = false;
            lbTextAutentificare.Enabled = false;
            lbNumeUtilizator.Visible = false;
            lbNumeUtilizator.Enabled = false;
            lbParola.Visible = false;
            lbParola.Enabled = false;

            txtNumeUtilizator.Text = "";
            txtParola.Text = "";
        }

        private void activeazaControale()
        {
            if (utilizator.isAdmin() == true)
            {
                administrareConturiToolStripMenuItem.Visible = true;
                administrareConturiToolStripMenuItem.Enabled = true;
                btnAudit.Visible = true;
                btnAudit.Enabled = true;
            }
            lbOperatii.Visible = true;
            lbOperatii.Enabled = true;
            lbStergere.Visible = true;
            lbStergere.Enabled = true;
          //  lbAudit.Visible = true;
            //lbAudit.Enabled = true;
            lbEditare.Visible = true;
            lbEditare.Enabled = true;
            //lbDataVersiunii.Visible = true;
           // lbDataVersiunii.Enabled = true;
            //lbFd.Enabled = true;
           // lbFd.Visible = true;
            lbProduse.Visible = true;
            lbProduse.Enabled = true;

            txtEditare.Visible = true;
            txtEditare.Enabled = true;
            txtStergere.Visible = true;
            txtStergere.Enabled = true;
            //txtFD.Visible = true;
           // txtFD.Enabled = true;

            btnAdaugare.Visible = true;
            btnAdaugare.Enabled = true;
            btnEditare.Visible = true;
            btnEditare.Enabled = true;
            btnSterge.Visible = true;
            btnSterge.Enabled = true;
            //btnBackup.Visible = true;
            //btnBackup.Enabled = true;
            btnAdminBrCat.Visible = true;
            btnAdminBrCat.Enabled = true;
           // btnRestaureaza.Enabled = true;
           // btnRestaureaza.Visible = true;

            checkBoxStergereDefinitiva.Visible = true;
            checkBoxStergereDefinitiva.Enabled = true;
            checkBoxTemporar.Visible = true;
            checkBoxTemporar.Enabled = true;

            activeazaControaleVizitator();

            pictureBox1.Visible = false;
            pictureBox1.Enabled = false;
        }

        private void dezactiveazaControale()
        {
            administrareConturiToolStripMenuItem.Visible = false;
            administrareConturiToolStripMenuItem.Enabled = false;

            lbOperatie.Visible = false;
            lbOperatie.Enabled = false;
            lbProduse.Visible = false;
            lbProduse.Enabled = false;
            lbStergere.Visible = false;
            lbStergere.Enabled = false;
            //lbAudit.Visible = false;
            //lbAudit.Enabled = false;
            lbEditare.Visible = false;
            lbEditare.Enabled = false;
           // lbDataVersiunii.Visible = false;
           // lbDataVersiunii.Enabled = false;
            //lbFd.Enabled = false;
            //lbFd.Visible = false;
            lbProduse.Visible = false;
            lbProduse.Enabled = false;

            txtEditare.Visible = false;
            txtEditare.Enabled = false;
            txtStergere.Visible = false;
            txtStergere.Enabled = false;
          //  txtFD.Visible = false;
            //txtFD.Enabled = false;

            btnAdaugare.Visible = false;
            btnAdaugare.Enabled = false;
            btnEditare.Visible = false;
            btnEditare.Enabled = false;
            btnSterge.Visible = false;
            btnSterge.Enabled = false;
            //btnBackup.Visible = false;
           // btnBackup.Enabled = false;
           // btnRestaureaza.Enabled = false;
           // btnRestaureaza.Visible = false;
            btnAdminBrCat.Visible = false;
            btnAdminBrCat.Enabled = false;
            btnAudit.Visible = false;
            btnAudit.Enabled = false;
  
            checkBoxStergereDefinitiva.Visible = false;
            checkBoxStergereDefinitiva.Enabled = false;
            checkBoxTemporar.Visible = false;
            checkBoxTemporar.Enabled = false;

            dezactiveazaControaleVizitator();
        }

        private void activeazaControaleVizitator()
        {
            pictureBox1.Visible = true;
            pictureBox1.Enabled = true;
            lbOperatie.Visible = true;
            lbOperatie.Enabled = true;
            lbProduse.Visible = true;
            lbProduse.Enabled = true;
            lbExport.Visible = true;
            lbExport.Enabled = true;
            lbFisDest.Visible = true;
            lbFisDest.Enabled = true;

            btnExecutaOperatie.Visible = true;
            btnExecutaOperatie.Enabled = true;
            btnExport.Visible = true;
            btnExport.Enabled = true;
            btnLogout.Visible = true;
            btnLogout.Enabled = true;
            txtFisierDest.Visible = true;
            txtFisierDest.Enabled = true;

            cbExport.Visible = true;
            cbExport.Enabled = true;
            cbOperatie.Visible = true;
            cbOperatie.Enabled = true;

            dgProduse.Visible = true;
            dgProduse.Enabled = true;
        }

        private void dezactiveazaControaleVizitator()
        {
            pictureBox1.Visible = false;
            pictureBox1.Enabled = false;
            lbOperatie.Visible = false;
            lbOperatie.Enabled = false;
            lbProdusDeCautat.Visible = false;
            lbProdusDeCautat.Enabled = false;
            lbProduse.Visible = false;
            lbProduse.Enabled = false;
            lbCriteriu.Visible = false;
            lbCriteriu.Enabled = false;
            lbCriteriuDeSortare.Visible = false;
            lbCriteriuDeSortare.Enabled = false;
            lbExport.Visible = false;
            lbExport.Enabled = false;
            //lbDataVersiunii.Visible = false;
           // lbDataVersiunii.Enabled = false;
            lbFisDest.Visible = false;
            lbFisDest.Enabled = false;
            lbLimitaInferioara.Visible = false;
            lbLimitaInferioara.Enabled = false;
            lbLimitaSuperioara.Enabled = false;
            lbLimitaSuperioara.Visible = false;

            btnExecutaOperatie.Visible = false;
            btnExecutaOperatie.Enabled = false;
            btnExport.Visible = false;
            btnExport.Enabled = false;
            btnLogout.Visible = false;
            btnLogout.Enabled = false;

            txtFisierDest.Visible = false;
            txtFisierDest.Enabled = false;


            cbFiltru.Visible = false;
            cbFiltru.Enabled = false;
            cbExport.Visible = false;
            cbExport.Enabled = false;
            cbOperatie.Visible = false;
            cbOperatie.Enabled = false;
            cbSorteazaDupa.Visible = false;
            cbSorteazaDupa.Enabled = false;
            cbOperatie.Visible = false;
            cbOperatie.Enabled = false;

            dgProduse.Visible = false;
            dgProduse.Enabled = false;
        }

        private void activeazaControaleAutenficare()
        {
            txtNumeUtilizator.Visible = true;
            txtNumeUtilizator.Enabled = true;
            txtParola.Visible = true;
            txtParola.Enabled = true;
            btnLogareCaVizitator.Enabled = true;
            btnLogareCaVizitator.Visible = true;
            btnLogin.Visible = true;
            btnLogin.Enabled = true;
            btnLogout.Visible = true;
            btnLogin.Enabled = true;
            lbAutentificare.Visible = true;
            lbAutentificare.Enabled = true;
            lbTextAutentificare.Visible = true;
            lbTextAutentificare.Enabled = true;
            lbNumeUtilizator.Visible = true;
            lbNumeUtilizator.Enabled = true;
            lbParola.Visible = true;
            lbParola.Enabled = true;
        }
    }
}